#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
const int N = 100005;
int n, m;
int deep, dfn[N], low[N], color[N];
vector <int> g[N];
stack <int> s;
int dis[N];
bool ins[N];
void Tarjan (int u) {
	deep++;
	dfn[u] = deep;
	low[u] = deep;
	s.push(u); ins[u]=1;
	for (unsigned i = 0; i < g[u].size(); i++) {
		int v = g[u][i];
		if (!dfn[v]) {
			Tarjan (v);
			low[u] = min (low[u], low[v]);
		} else if (ins[v]) {
			low[u] = min (low[u], low[v]);
		}
	}
	if (low[u] == dfn[u]) {
		color[u] = u;
		while (s.top() != u) {
			int top = s.top();
			color[top] = u;	
            ins[top] = 0;
			s.pop();
		}
        ins[u] = 0;
		s.pop();
	}
}
int main() {
	scanf ("%d%d", &n, &m);
	int a, b;
	for (int i = 1; i <= m; i++) {
		scanf ("%d%d", &a, &b);
		g[a].push_back(b);
	}
    for(int i = 1; i <= n; i++) {
    	if (!dfn[i]) {
    		Tarjan(i);
		} 
	}
	int t;
	scanf ("%d", &t);
	for (int i = 1; i <= n; i++) {
		scanf ("%d%d", &a, &b);
		if (color[a] == color[b]) {
			printf ("1\n");
		} else {
			printf ("0\n");
		}
	}
	return 0;
}
